using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Matrix.Notification.Domain.Notifications;
using Matrix.Notification.Channels;

namespace Matrix.Notification.Services
{
    /// <summary>
    /// 通知管理器接口
    /// </summary>
    public interface INotificationManager
    {
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="message">通知消息</param>
        /// <param name="channels">指定渠道（可选，不指定则使用智能路由）</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发送结果</returns>
        Task<NotificationSendResult> SendAsync(
            NotificationMessage message,
            IEnumerable<NotificationChannel>? channels = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 批量发送通知
        /// </summary>
        /// <param name="messages">通知消息列表</param>
        /// <param name="channels">指定渠道（可选）</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发送结果</returns>
        Task<NotificationSendResult> SendBatchAsync(
            IEnumerable<NotificationMessage> messages,
            IEnumerable<NotificationChannel>? channels = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据用户偏好发送通知
        /// </summary>
        /// <param name="message">通知消息</param>
        /// <param name="userId">用户ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发送结果</returns>
        Task<NotificationSendResult> SendToUserAsync(
            NotificationMessage message,
            Guid userId,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据用户偏好批量发送通知
        /// </summary>
        /// <param name="message">通知消息</param>
        /// <param name="userIds">用户ID列表</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发送结果</returns>
        Task<NotificationSendResult> SendToUsersAsync(
            NotificationMessage message,
            IEnumerable<Guid> userIds,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 发送延迟通知
        /// </summary>
        /// <param name="message">通知消息</param>
        /// <param name="scheduledTime">计划发送时间</param>
        /// <param name="channels">指定渠道（可选）</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>任务ID</returns>
        Task<string> SendDelayedAsync(
            NotificationMessage message,
            DateTime scheduledTime,
            IEnumerable<NotificationChannel>? channels = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取可用的通知渠道
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>可用渠道列表</returns>
        Task<List<NotificationChannelInfo>> GetAvailableChannelsAsync(
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取通知发送状态
        /// </summary>
        /// <param name="notificationId">通知ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发送状态</returns>
        Task<NotificationStatus?> GetStatusAsync(
            string notificationId,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 取消待发送的通知
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>是否成功取消</returns>
        Task<bool> CancelAsync(
            string taskId,
            CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// 通知发送结果
    /// </summary>
    public class NotificationSendResult
    {
        /// <summary>
        /// 通知ID
        /// </summary>
        public string NotificationId { get; set; } = string.Empty;

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 总发送数量
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 成功发送数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败发送数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalCount > 0 ? (double)SuccessCount / TotalCount : 0;

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime SentTime { get; set; }

        /// <summary>
        /// 耗时（毫秒）
        /// </summary>
        public long DurationMs { get; set; }

        /// <summary>
        /// 各渠道发送结果
        /// </summary>
        public Dictionary<NotificationChannel, ChannelSendResult> ChannelResults { get; set; } = new();

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 额外元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 渠道发送结果
    /// </summary>
    public class ChannelSendResult
    {
        /// <summary>
        /// 渠道类型
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// 发送数量
        /// </summary>
        public int SentCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 耗时（毫秒）
        /// </summary>
        public long DurationMs { get; set; }
    }

    /// <summary>
    /// 通知渠道信息
    /// </summary>
    public class NotificationChannelInfo
    {
        /// <summary>
        /// 渠道类型
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 渠道名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 渠道描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 是否可用
        /// </summary>
        public bool IsAvailable { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnabled { get; set; }

        /// <summary>
        /// 支持的通知类型
        /// </summary>
        public List<NotificationType> SupportedTypes { get; set; } = new();

        /// <summary>
        /// 预估延迟（毫秒）
        /// </summary>
        public int EstimatedLatencyMs { get; set; }

        /// <summary>
        /// 可靠性评分（0-100）
        /// </summary>
        public int ReliabilityScore { get; set; }

        /// <summary>
        /// 配置状态
        /// </summary>
        public ConfigurationStatus ConfigStatus { get; set; }
    }

    /// <summary>
    /// 通知状态
    /// </summary>
    public class NotificationStatus
    {
        /// <summary>
        /// 通知ID
        /// </summary>
        public string NotificationId { get; set; } = string.Empty;

        /// <summary>
        /// 状态
        /// </summary>
        public NotificationStatusValue Status { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime? SentTime { get; set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime? CompletedTime { get; set; }

        /// <summary>
        /// 各渠道状态
        /// </summary>
        public Dictionary<NotificationChannel, ChannelStatus> ChannelStatuses { get; set; } = new();

        /// <summary>
        /// 总发送数量
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdatedTime { get; set; }
    }

    /// <summary>
    /// 渠道状态
    /// </summary>
    public class ChannelStatus
    {
        /// <summary>
        /// 渠道类型
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public NotificationStatusValue Status { get; set; }

        /// <summary>
        /// 发送数量
        /// </summary>
        public int SentCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public int FailureCount { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime? CompletedTime { get; set; }
    }

    /// <summary>
    /// 通知状态值
    /// </summary>
    public enum NotificationStatusValue
    {
        /// <summary>
        /// 等待中
        /// </summary>
        Pending,

        /// <summary>
        /// 发送中
        /// </summary>
        Sending,

        /// <summary>
        /// 部分成功
        /// </summary>
        PartialSuccess,

        /// <summary>
        /// 发送成功
        /// </summary>
        Success,

        /// <summary>
        /// 发送失败
        /// </summary>
        Failed,

        /// <summary>
        /// 已取消
        /// </summary>
        Canceled,

        /// <summary>
        /// 重试中
        /// </summary>
        Retrying
    }

    /// <summary>
    /// 配置状态
    /// </summary>
    public enum ConfigurationStatus
    {
        /// <summary>
        /// 未配置
        /// </summary>
        NotConfigured,

        /// <summary>
        /// 配置不完整
        /// </summary>
        Incomplete,

        /// <summary>
        /// 配置错误
        /// </summary>
        Error,

        /// <summary>
        /// 配置完成
        /// </summary>
        Configured,

        /// <summary>
        /// 连接测试成功
        /// </summary>
        Connected,

        /// <summary>
        /// 连接失败
        /// </summary>
        ConnectionFailed
    }
}